Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
react-side-effect
Advanced tools
The react-side-effect package is a utility for creating higher-order components that handle side effects in React applications. It allows you to manage side effects in a declarative way, ensuring that they are handled consistently and predictably.
Creating a Side Effect Component
This feature allows you to create a higher-order component that manages side effects. The `reducePropsToState` function combines all props into a single state object, and the `handleStateChangeOnClient` function handles the side effect on the client.
const withSideEffect = require('react-side-effect');
function reducePropsToState(propsList) {
// Combine all props into a single state object
return propsList.reduce((acc, props) => ({ ...acc, ...props }), {});
}
function handleStateChangeOnClient(state) {
// Handle the side effect on the client
console.log('State changed:', state);
}
const SideEffectComponent = withSideEffect(reducePropsToState, handleStateChangeOnClient)(MyComponent);
Using the Side Effect Component
Once you have created the side effect component, you can use it in your React application like any other component. The side effects will be managed according to the logic defined in the higher-order component.
<SideEffectComponent prop1="value1" prop2="value2" />
react-helmet is a package that manages changes to the document head, such as title and meta tags, in a declarative way. It is similar to react-side-effect in that it handles side effects, but it is specifically focused on managing the document head.
redux-saga is a middleware library for managing side effects in Redux applications. It uses generator functions to handle asynchronous actions and side effects. While it is more complex than react-side-effect, it provides powerful tools for managing side effects in a Redux-based application.
react-use is a collection of React hooks that includes hooks for managing side effects, such as useEffectOnce and useAsync. It provides a more granular approach to managing side effects compared to react-side-effect, which focuses on higher-order components.
Create components whose prop changes map to a global side effect.
npm install --save react-side-effect
<script src="https://unpkg.com/react/umd/react.development.js" type="text/javascript"></script>
<script src="https://unpkg.com/react-side-effect/lib/index.umd.js" type="text/javascript"></script>
<script src="https://unpkg.com/react/umd/react.production.min.js" type="text/javascript"></script>
<script src="https://unpkg.com/react-side-effect/lib/index.umd.min.js" type="text/javascript"></script>
document.body.style.margin
or background color depending on current screen;componentDidUpdate
?It gathers current props across the whole tree before passing them to side effect. For example, this allows you to create <BodyStyle style>
component like this:
// RootComponent.js
return (
<BodyStyle style={{ backgroundColor: 'red' }}>
{this.state.something ? <SomeComponent /> : <OtherComponent />}
</BodyStyle>
);
// SomeComponent.js
return (
<BodyStyle style={{ backgroundColor: this.state.color }}>
<div>Choose color: <input valueLink={this.linkState('color')} /></div>
</BodyStyle>
);
and let the effect handler merge style
from different level of nesting with innermost winning:
import { Component, Children } from 'react';
import PropTypes from 'prop-types';
import withSideEffect from 'react-side-effect';
class BodyStyle extends Component {
render() {
return Children.only(this.props.children);
}
}
BodyStyle.propTypes = {
style: PropTypes.object.isRequired
};
function reducePropsToState(propsList) {
var style = {};
propsList.forEach(function (props) {
Object.assign(style, props.style);
});
return style;
}
function handleStateChangeOnClient(style) {
Object.assign(document.body.style, style);
}
export default withSideEffect(
reducePropsToState,
handleStateChangeOnClient
)(BodyStyle);
On the server, you’ll be able to call BodyStyle.peek()
to get the current state, and BodyStyle.rewind()
to reset for each next request. The handleStateChangeOnClient
will only be called on the client.
withSideEffect: (reducePropsToState, handleStateChangeOnClient, [mapStateOnServer]) -> ReactComponent -> ReactComponent
A higher-order component that, when mounting, unmounting or receiving new props, calls reducePropsToState
with props
of each mounted instance. It is up to you to return some state aggregated from these props.
On the client, every time the returned component is (un)mounted or its props change, reducePropsToState
will be called, and the recalculated state will be passed to handleStateChangeOnClient
where you may use it to trigger a side effect.
On the server, handleStateChangeOnClient
will not be called. You will still be able to call the static rewind()
method on the returned component class to retrieve the current state after a renderToString()
call. If you forget to call rewind()
right after renderToString()
, the internal instance stack will keep growing, resulting in a memory leak and incorrect information. You must call rewind()
after every renderToString()
call on the server.
For testing, you may use a static peek()
method available on the returned component. It lets you get the current state without resetting the mounted instance stack. Don’t use it for anything other than testing.
Here's how to implement React Document Title (both client and server side) using React Side Effect:
import React, { Children, Component } from 'react';
import PropTypes from 'prop-types';
import withSideEffect from 'react-side-effect';
class DocumentTitle extends Component {
render() {
if (this.props.children) {
return Children.only(this.props.children);
} else {
return null;
}
}
}
DocumentTitle.propTypes = {
title: PropTypes.string.isRequired
};
function reducePropsToState(propsList) {
var innermostProps = propsList[propsList.length - 1];
if (innermostProps) {
return innermostProps.title;
}
}
function handleStateChangeOnClient(title) {
document.title = title || '';
}
export default withSideEffect(
reducePropsToState,
handleStateChangeOnClient
)(DocumentTitle);
FAQs
Create components whose prop changes map to a global side effect
The npm package react-side-effect receives a total of 863,916 weekly downloads. As such, react-side-effect popularity was classified as popular.
We found that react-side-effect demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.